package com.ap.core.business.message.response.translators.impl;

import com.ap.core.backoffice.domain.BackOfficeOrderPayment;
import com.ap.core.business.message.domain.translators.BindingMetaData;
import com.ap.core.business.message.response.translators.ResponseMetaData;
import com.ap.core.business.message.response.translators.ResponseTranslator;
import com.ap.core.business.message.response.translators.listener.ResponseListener;
import com.ap.core.cxf.jaxb.distributor.DistributorInfoType;
import com.ap.core.cxf.jaxb.distributor.MerchantDetailsType;
import com.ap.core.cxf.jaxb.managerefund.RefundAcctDetailsType;
import com.ap.core.cxf.jaxb.managerefund.RefundInfoType;
import com.ap.core.dao.DistributorDao;
import com.ap.core.dao.FinancialInstitutionDao;
import com.ap.core.dao.MerchantDao;
import com.ap.core.domain.Distributor;
import com.ap.core.domain.FinancialInstitution;
import com.ap.core.domain.Merchant;
import com.ap.core.domain.OrderPaymentRefund;
import com.ap.core.message.managerefund.info.RequestRetrieveRefundAccountInfo;
import com.ap.core.message.managerefund.info.ResponseRetrieveRefundAccountInfo;

/**
 * User: Ranjit.Singh
 * implementation class for {@link com.ap.core.business.message.response.translators.ResponseTranslator} to populate {@link com.ap.core.cxf.jaxb.common.ResponseWithIntentType}  in response object
 */
public class ResponseRetrieveRefundAccountTranslator implements ResponseTranslator<RequestRetrieveRefundAccountInfo, ResponseRetrieveRefundAccountInfo> {

    private DistributorDao distributorDao;
    private MerchantDao merchantDao;
    private FinancialInstitutionDao financialInstitutionDao;

    /**
     * {@inheritDoc}
     */
    @Override
    public void populateResponse(RequestRetrieveRefundAccountInfo request, ResponseRetrieveRefundAccountInfo response, BindingMetaData bindingMetaData, ResponseMetaData responseMetaData, ResponseListener responseListener) {
        final BackOfficeOrderPayment originalOrderPayment = responseMetaData.getBackOfficeOrderPayment();      
        
        final OrderPaymentRefund  orderPaymentRefund = responseMetaData.getOrderPaymentRefund();

        final DistributorInfoType distributorInfoType = new DistributorInfoType();
        final Distributor distributor = distributorDao.findById(originalOrderPayment.getDistributorId());

        distributorInfoType.setDistributorId(distributor.getDistributorIdentification());
        distributorInfoType.setDistributorName(distributor.getDistributorName());

        response.setDistributorInfo(distributorInfoType);
        

        final MerchantDetailsType merchantDetailsType = new MerchantDetailsType();
        final Merchant merchant = merchantDao.findById(originalOrderPayment.getMerchantId());
        merchantDetailsType.setMerchantId(merchant.getMerchantIdentification());
        merchantDetailsType.setMerchantCmnName(merchant.getCommonName());
        merchantDetailsType.setMerchantName(merchant.getName());

        response.setMerchantInfo(merchantDetailsType);
        
        
        RefundInfoType refundInfo = new RefundInfoType();
        refundInfo.setApTransactionId(request.getRefundRequestInfo().getApTransactionId());
        refundInfo.setApTRId(request.getRefundRequestInfo().getApTRId());
        refundInfo.setCurrency(request.getRefundRequestInfo().getCurrency());
        refundInfo.setMerchantRefundRef(request.getRefundRequestInfo().getMerchantRefundRef());
        refundInfo.setRefundAmount(request.getRefundRequestInfo().getRefundAmount());
        refundInfo.setRefundMethod(request.getRefundRequestInfo().getRefundMethod());
        refundInfo.setRefundReasonType(request.getRefundRequestInfo().getRefundReasonType());
        refundInfo.setRefundTransactionId(orderPaymentRefund.getId());
        refundInfo.setRefundTRId(orderPaymentRefund.getApTransactionRetrievalId().toString());
        response.setRefundInfo(refundInfo);
        
        
        FinancialInstitution financialInstitution = financialInstitutionDao.findById(orderPaymentRefund.getFiId());


        RefundAcctDetailsType refundAccDetails = new RefundAcctDetailsType();
        if(orderPaymentRefund.getAccountNumber() != null){
        	refundAccDetails.setAcctNum(orderPaymentRefund.getAccountNumber().toString());
        }
        if(orderPaymentRefund.getSortCode() != null){
        	refundAccDetails.setAcctSortCode(orderPaymentRefund.getSortCode().toString());
        }
        refundAccDetails.setFIId(financialInstitution.getFiIdentification());
        refundAccDetails.setFIShortName(financialInstitution.getShortName());
        if(orderPaymentRefund.getRollNumber() != null){
        	refundAccDetails.setRollNum(orderPaymentRefund.getRollNumber().toString());
        }
        refundAccDetails.setFIConsumerId(orderPaymentRefund.getFiCustomerId());

        response.setRefundAcctDetails(refundAccDetails);

        responseListener.onMessageResponse(response);

    }

    public void setDistributorDao(DistributorDao distributorDao) {
        this.distributorDao = distributorDao;
    }

    public void setMerchantDao(MerchantDao merchantDao) {
        this.merchantDao = merchantDao;
    }

    public void setFinancialInstitutionDao(FinancialInstitutionDao financialInstitutionDao) {
        this.financialInstitutionDao = financialInstitutionDao;
    }
    

}
